package com.dmall.scf.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Created by yzm on 2019/5/21.
 */
@Slf4j
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    /**
     * 日期+时间的格式
     */
    public final static  String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static  String DATE_TIME = "yyyy-MM-dd HH:mm";
    public final static String DATETIME_MILL_SECOND_FILE = "yyyyMMddHHmmssSSS";
    public final static String DATE_SHORT_PATTERN = "yyyyMMdd";

    /**
     * 日期的格式
     */
    final static public String YYYY_MM_DD = "yyyy-MM-dd";
    final static public String YYYYMMDD = "yyyyMMdd";
    final static public String DATETIME_FORMAT_SIMPLE = "yyyyMMdd HH:mm:ss";
    final static public String MONTH_FORMAT = "yyyy-MM";
    final static public String YYYY_DOT_MM = "yyyy.MM";

    /**
     * 中文日期的格式
     */
    final static public String DATE_CH_FORMAT = "yyyy年MM月dd日";
    final static public String FORMAT_MMDD = "MMdd";

    /**
     * 小时:分的格式
     */
    final static public String HHMM_FORMAT = "HH:mm";
    final static public String HHMMSS_FORMAT = "HH:mm:ss";
    final static public String HHMMSS_SIMPLE_FORMAT = "HHmmss";
    final static public String DATE_TIME_SSS_FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
    private static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";

    /**
     * 年
     */
    final static public String YYYY = "yyyy";

    public static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
    public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);

    public static String dateToStr(Date date) {
        return dateToStr(date, null);
    }

    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        if (StringUtils.isBlank(formatStr)) {
            formatStr = YYYY_MM_DD;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }


    /**
     * 获取最近时间
     *
     * @param offSet
     * @param type
     * @return
     */
    public static String getLastDateTime(Integer offSet, DateTimeType type) {
        String result = null;
        DateTime dateTime = new DateTime();
        switch (type) {
            case MINUTE:
                result = dateTimeToStr(dateTime.minusMinutes(offSet),DATE_TIME_FORMAT);
                break;
            case HOUR:
                result = dateTimeToStr(dateTime.minusHours(offSet),DATE_TIME_FORMAT);
                break;
            case DAY:
                result = dateTimeToStr(dateTime.minusDays(offSet),YYYY_MM_DD);
                break;
            case WEEK:
                result = dateTimeToStr(dateTime.minusWeeks(offSet),YYYY_MM_DD);
                break;
            case YEAR:
                result = dateTimeToStr(dateTime.minusYears(offSet),YYYY_MM_DD);
                break;
        }
        return result;
    }

    public static String dateTimeToStr(DateTime date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        if (StringUtils.isBlank(formatStr)) {
            formatStr = DATE_TIME_FORMAT;
        }
        return date.toString(formatStr);
    }


    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate4Format(){
        return LocalDateTime.now().format(dateFormatter);
    }


    /**
     * 获取当前日期+时间
     * @return
     */
    public static String getCurrentTime4Format(){
        return LocalDateTime.now().format(dateTimeFormatter);
    }

    /**
     * 加几天
     * @param days
     * @return
     */
    public static LocalDate addDays(int days){
        return LocalDate.now().plusDays(days);
    }

    /**
     * 加几天
     * @param days
     * @return
     */
    public static String addSomeDays(int days){
        return LocalDate.now().plusDays(days).format(dateFormatter);
    }

    //判断“YYYY-MM-DD”类型 时间格式
    public static boolean isDate(String date) {
        Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
        return p.matcher(date).matches();
    }

    public static String getCron(final Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
        String formatTimeStr = "";
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }


    public static Date min(Date... dates) {
        if (dates == null || dates.length == 0) return null;
        Date minDate = dates[0];
        for (Date tmp : dates) {
            if (tmp.before(minDate)) {
                minDate = tmp;
            }
        }
        return minDate;
    }

    public static Date min(List<Date> dates) {
        if (CollectionUtils.isEmpty(dates)) return null;
        return min(dates.toArray(new Date[0]));
    }

    public static Date max(Date... dates) {
        if (dates == null || dates.length == 0) return null;
        Date maxDate = dates[0];
        for (Date tmp : dates) {
            if (tmp.after(maxDate)) {
                maxDate = tmp;
            }
        }
        return maxDate;
    }

    public static Date max(List<Date> dates) {
        if (CollectionUtils.isEmpty(dates)) return null;
        return max(dates.toArray(new Date[0]));
    }

    public static String getRandomPathForFile() {
        DateTime dateTime = new DateTime();
        return dateTime.getYear() + "" + dateTime.getMonthOfYear() + "" + dateTime.getDayOfMonth() + "";
    }

    /**
     * 判断日期是否为同一天
     *
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * 将日期字符串解析成指定格式的Date对象
     *
     * @param dateTime 日期字符串
     * @param format   指定格式
     * @return （正确格式）日期对象
     * @throws ParseException
     */
    public static Date parse(String dateTime, String format) {
        if (org.apache.commons.lang.StringUtils.isBlank(dateTime) || org.apache.commons.lang.StringUtils.isBlank(format))
            return null;
        String sDateTime = ((dateTime.indexOf('.') > 0)) ? dateTime.substring(
                0, dateTime.indexOf('.')) : dateTime;

        DateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(sDateTime);
        } catch (Exception e) {
            log.error("parse date {} with {} exception: {}", dateTime, format, e.getMessage());
            return null;
        }
    }

    public static Date parseDate(String str) {
        return parse(str, YYYYMMDD);
    }

    /**
     * 将日期类解析成指定格式的日期字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format) {
        if (date == null || org.apache.commons.lang.StringUtils.isBlank(format))
            return "";
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 将日期字符串解析成"yyyy-MM-dd"格式的Date对象
     *
     * @param dateTime 日期字符串
     *                 指定格式
     * @return （正确格式）日期对象
     */
    public static Date parseDay(String dateTime) {
        return parse(dateTime, YYYY_MM_DD);
    }

    public static Date parseDay(String dateStr, String format) {
        return parse(dateStr, format);
    }

    /**
     * 将日期字符串解析成"yyyy年MM月dd日"格式的Date对象
     *
     * @param dateTime 日期字符串
     *                 指定格式
     * @return （正确格式）日期对象
     */
    public static Date parseChDay(String dateTime) {
        return parse(dateTime, DATE_CH_FORMAT);
    }

    /**
     * 将日期类解析成"yyyy-MM-dd"格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDay(Date date) {
        return format(date, YYYY_MM_DD);
    }

    /**
     * 将日期类解析成"yyyyMMdd"格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatSimpleDay(Date date) {
        return format(date, YYYYMMDD);
    }

    /**
     * 将日期类解析成"yyyy年MM月dd日"格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatChYYYYMMDD(Date date) {
        return format(date, DATE_CH_FORMAT);
    }

    /**
     * 将日期类解析成"yyyy-MM-dd HH:mm:ss"格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return format(date, DATE_TIME_FORMAT);
    }

    /**
     * 将日期类解析成"yyyy-MM-dd HH:mm:ss"格式的日期字符串
     * @param date
     * @return
     */
    public static String formatTimeStr(String date, String format) {
        return format( DateUtils.parse(date, format), DATE_TIME_FORMAT);
    }


    /**
     * 将日期类解析成"yyyy-MM-dd"格式的日期字符串
     * @param date
     * @return
     */
    public static String formatNormalDateStr(String date) {
        return formatDateStr(date, YYYY_MM_DD);
    }

    /**
     * 将日期类解析成"yyyyMMdd"格式的日期字符串
     * @param date
     * @return
     */
    public static String formatShortDateStr(String date) {
        return formatDateStr(date, DATE_SHORT_PATTERN);
    }

    /**
     * 将日期类解析成"yyyyMMdd"格式的日期字符串
     * @param date
     * @return
     */
    public static String formatNormal2ShortDateStr(String date) {
        return  format( DateUtils.parse(date, YYYY_MM_DD), DATE_SHORT_PATTERN);
    }

    /**
     * 将日期类解析成日期字符串
     * @param date
     * @return
     */
    public static String formatDateStr(String date, String format) {
        return format(DateUtils.parse(date, format), format);
    }

    public static String formatMinute(Date date) {
        return format(date, DATE_TIME);
    }

    /**
     * 上一天
     *
     * @return
     */
    public static Date getPrevDay(Date date) {
        if (date == null) {
            return null;
        }
        return truncateDay(addDays(date, -1));
    }

    /**
     * 按天数获取日期，负数表示获取date之前的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date getDays(Date date, Integer days) {
        if (date == null) return null;
        return truncateDay(addDays(date, days));
    }

    /**
     * 后一天
     *
     * @return
     */
    public static Date getNextDay(Date date) {
        if (date == null) {
            return null;
        }
        return truncateDay(addDays(date, 1));
    }

    /**
     * d0是否在[start,end]的日期区间中,闭区间
     *
     * @return
     */
    public static boolean between(Date d0, Date start, Date end) {
        if (d0 == null) {
            return false;
        }
        boolean after = start == null || d0.compareTo(start) >= 0;
        boolean before = end == null || d0.compareTo(end) <= 0;
        return after && before;
    }

    /**
     * 返回日期
     *
     * @param year  1900-...
     * @param month 0-11
     * @return
     * @throws RuntimeException
     */
    public static Date toDate(int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        return calendar.getTime();
    }

    public static Date fromSecond(Long second) {
        if (second == null || second <= 0l) {
            return null;
        }
        return new Date(second * 1000l);
    }

    /**
     * 返回四位年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        if (date == null)
            return -1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 返回当前四位年份
     * @return
     */
    public static String getCurrentYear() {
        DateTime dateTime = DateTime.now();
        return dateTime.toString(YYYY);
    }

    /**
     * 获取当年第一天
     * @return
     */
    public static String getFirstDateOfCurrentYear(){
        DateTime dateTime = DateTime.now();
        return dateTime.toString(YYYY)+"-01-01";
    }

    /**
     * 返回月数(0-11)
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        if (date == null)
            return -1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH);
    }

    /**
     * 获取去年第一天字符串
     * @return
     */
    public static String getLastYearFirstStr(String format){
        return format(getLastYearFirst(), format);
    }

    /**
     * 获取去年最后一天字符串
     * @return
     */
    public static String getLastYearLastStr(String format){
        return format(getLastYearLast(), format);
    }

    /**
     * 获取去年第一天
     * @return
     */
    public static Date getLastYearFirst(){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -1);
        return getYearFirst(calendar.get(Calendar.YEAR));
    }

    /**
     * 获取去年最后一天
     * @return
     */
    public static Date getLastYearLast(){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -1);
        return getYearLast(calendar.get(Calendar.YEAR));
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }


    /**
     * 返回月几(1-31)
     *
     * @param date
     * @return
     */
    public static int getMonthDay(Date date) {
        if (date == null)
            return -1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 返回周几。周日为0，周一为1
     *
     * @param date
     * @return 0：周日；1：周一...
     */
    public static int getWeekDay(Date date) {
        if (date == null)
            return -1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public static Date startOfDay(Date date) {
        return new DateTime(date).withTimeAtStartOfDay().toDate();
    }

    public static Date startOfHour(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            throw new RuntimeException("parse error", e);
        }
    }

    public static Date startOfDay(Long date) {
        return new DateTime(date).withTimeAtStartOfDay().toDate();
    }

    public static Date endOfDay(Date date) {
        return new DateTime(date).withTimeAtStartOfDay().plusDays(1).minusSeconds(1).toDate();
    }

    public static Date endOfDay(Long date) {
        return new DateTime(date).withTimeAtStartOfDay().plusDays(1).minusSeconds(1).toDate();
    }

    /**
     * 获取 GMT 格式时间戳
     * return String;
     */
    public static String formatGMTDate(Date date) {
        SimpleDateFormat formater = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        formater.setTimeZone(TimeZone.getTimeZone("GMT"));
        return formater.format(date);
    }

    public static Date[] getDayStartEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Date[] dates = new Date[2];
        calendar = truncate(calendar, Calendar.DAY_OF_MONTH);
        dates[0] = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.add(Calendar.MILLISECOND, -1);
        dates[1] = calendar.getTime();
        return dates;
    }

    /**
     * 取得一个date对象对应的日期的0点0分0秒时刻的Date对象。
     *
     * @throws
     * @Title: getMinDateOfDay
     * @Description: 取得一个date对象对应的日期的0点0分0秒时刻的Date对象。
     * @param: @param date 给定的日期
     * @param: @return 操作后的日期
     * @return: Date
     * @author lc
     * @Date 2015-11-4 下午5:38:43
     */
    public static Date getMinDateOfDay(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        return calendar.getTime();
    }

    /**
     * 取得一个date对象对应的日期的23点59分59秒时刻的Date对象。
     *
     * @throws
     * @Title: getMaxDateOfDay
     * @Description: 取得一个date对象对应的日期的23点59分59秒时刻的Date对象。
     * @param: @param date 给定的日期
     * @param: @return 处理之后的日期
     * @return: Date
     * @author lc
     * @Date 2015-11-4 下午5:39:24
     */
    public static Date getMaxDateOfDay(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return calendar.getTime();
    }

    /**
     * 获取某月的所有天
     *
     * @param monthStr
     * @param fmt
     * @return
     */
    public static List<Date> allMonthDay(String monthStr, String fmt) {
        List<Date> days = new ArrayList<>(31);
        Date day = DateUtils.parse(monthStr, fmt);
        if (day == null) {
            return Collections.emptyList();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        int month = calendar.get(Calendar.MONTH);
        while (calendar.get(Calendar.MONDAY) == month) {
            days.add(calendar.getTime());
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }

    public static int diffDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return -1;
        }
        long time = date1.getTime() - date2.getTime();
        return (int) (time / (24 * 3600 * 1000l));
    }

    public static Date truncateDay(Date date) {
        if (date == null) {
            return null;
        }
        return truncate(date, Calendar.DAY_OF_MONTH);
    }

    public static int get(Date day, int field) {
        if (day == null || field < 0) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        return calendar.get(field);
    }


    public static String format(Long date) {
        Date date1 = new Date(date);
        return DateUtils.format(date1, YYYY_MM_DD);
    }

    /**
     * 往前推天数
     *
     * @param num
     * @return
     */
    public static Date frontManyDay(Date date, int num) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.DAY_OF_MONTH, 0 - num);
        return cl.getTime();
    }

    /**
     * 往后推天数
     *
     * @param num
     * @return
     */
    public static Date afterManyDay(Date date, int num) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.DAY_OF_MONTH, num);
        return cl.getTime();
    }


    public static String formatSimpleHHmmss(Date date) {
        return format(date, HHMMSS_SIMPLE_FORMAT);
    }

    /**
     * 获取当前月份前几个月时间列表，包括当前月份
     *
     * @param num 月份数量
     * @return
     */
    public static List<String> getPassedMonths(int num) {
        DateFormat df = new SimpleDateFormat("yyyy-MM");
        List<String> months = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, i - num + 1);
            String month = df.format(calendar.getTime());
            months.add(month);
        }
        return months;
    }

    /**
     * 获取当前月份前几个月时间列表，包括当前月份
     *
     * @param base 基础时间
     * @param num  月份数量
     * @return
     */
    public static List<String> getPassedMonths(Date base, int num) {
        DateFormat df = new SimpleDateFormat("yyyy-MM");
        List<String> months = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(base);
            calendar.add(Calendar.MONTH, i - num + 1);
            String month = df.format(calendar.getTime());
            months.add(month);
        }
        return months;
    }

    public static Date[] getRangeByMonthCount(int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1 - count);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Date[] dates = new Date[2];
        try {
            dates[0] = df.parse(df.format(calendar.getTime()));
            calendar.setTime(new Date());
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            dates[1] = df.parse(df.format(calendar.getTime()));
            return dates;
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }


    /**
     * 获取下月第一天
     *
     * @param recentDate
     * @return
     */
    public static Date getFirstDayOfNextMonth(Date recentDate) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(recentDate);
        ca.add(Calendar.MONTH, 1);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        return ca.getTime();
    }

    /**
     * 日期格式转换，由yy-mm-dd转换为mmdd
     *
     * @param dateStr:yy-mm-dd 格式
     * @return mmdd格式
     */
    public static String formatChangeToMMdd(String dateStr) {
        DateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD);
        try {
            Date date = dateFormat.parse(dateStr);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_MMDD);
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            log.error("parse date {} with {} exception: {}", dateStr, YYYY_MM_DD, e.getMessage());
            return "0000";
        }
    }

    /**
     * 计算两个时间相差的分钟数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getDiffMin(Date date1, Date date2) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT);
        //从对象中拿到时间
        long date1Time = date1.getTime();
        long date2Time = 0;
        try {
            date2Time = df.parse(df.format(date2)).getTime();
        } catch (ParseException e) {
            log.error("parse error");
        }
        return (date1Time - date2Time) / 1000 / 60;
    }

    /**
     * 获取两个时间间隔的所有日期
     * @param startTime "yyyy-MM-dd"
     * @param endTime "yyyy-MM-dd"
     * @return List("yyyy-MM-dd")
     */
    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    public static void main(String[] args) {
        System.out.println(getDays("2019-11-28", "2020-03-01"));
    }
}
